package corsi.hibernate.demo.web.transazioni.servlet;

import java.io.IOException;
import java.rmi.RMISecurityManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class ServetJBossDatasource
 */
public class ServetJBossDatasource extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	static Logger logger =Logger.getLogger("ServetJBossDatasource.class");
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServetJBossDatasource() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		logger.info("In Servlet JBoss Datasource");
		
		 try {

	        	// configurazione servizio Jndi JBOSS
	        	Hashtable env = new Hashtable();
	        	env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
	        	env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
	        	env.put("java.naming.provider.url","jnp://localhost:1099");
  	       	
	        	
	        	
	        	Context contestoDatasurce= new InitialContext(env);
	            DataSource datasourceJbossMySql;
	            datasourceJbossMySql = (DataSource) contestoDatasurce.lookup("jtaDs");
	            Connection connessione =datasourceJbossMySql.getConnection();
	            Statement statement = connessione.createStatement();
	            ResultSet cursore = statement.executeQuery("Select * from CD");
	           
	            while (cursore.next()){	            
	            	logger.info("\n"+cursore.getString("Artista"));
	            }
	            
	            cursore.close();
	            connessione.close();

	        } catch (NamingException ex) {
	        
	        	ex.printStackTrace();
	           // Logger.getLogger(TestDataSourceJboss.class.getName())..log(ex);
	        } catch (SQLException ecc){
	        
	        	logger.info("\n"+ecc);
	        }
		//DataSource ds = (DataSource)this.getServletContext().getResource("jdbc/jtaDs");
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
